#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2013-9-5

@author: iori
'''


import sys
import re
def safe_filename(text, max_length=200):
    """
    Sanitizes filenames for many operating systems.
    Keyword arguments:
    text -- The unsanitized pending filename.
    """
    #Quickly truncates long filenames.
    truncate = lambda text: text[:max_length].rsplit(' ', 0)[0]

    #Tidy up ugly formatted filenames.
    text = text.replace('_', ' ')
    text = text.replace(':', ' -')

    #NTFS forbids filenames containing characters in range 0-31 (0x00-0x1F)
    ntfs = [chr(i) for i in range(0, 31)]

    #Removing these SHOULD make most filename safe for a wide range
    #of operating systems.
    paranoid = ['\"', '\#', '\$', '\%', '\'', '\*', '\,', '\.', '\/', '\:',
                '\;', '\<', '\>', '\?', '\\', '\^', '\|', '\~', '\\\\']

    blacklist = re.compile('|'.join(ntfs + paranoid), re.UNICODE)
    filename = blacklist.sub('', text)
    return truncate(filename)

def progress_bar(current, total):
    ''' 25 # represent 100% '''
    if not hasattr(progress_bar, 'counter'):
        counter = -1
    percent = current*100/total
    if percent > counter:
        counter = percent
        animate = '[%s%s]%d%%\r'%('#'*(percent/4), ' '*(25-percent/4), percent)
        sys.stdout.write(animate)
        sys.stdout.flush()
        if percent == 100:
            sys.stdout.write('\n') # done return
        

def main():
    pass
if __name__ == '__main__':
    main()
    